import pytest
import requests
from pathlib import Path
from libs.parser import parser_files, write_files
root_path = Path().cwd()
@pytest.fixture(scope='session', autouse=True)
def save_root_path():

    data = {
        'TEST_DATA' : str(root_path / 'data'),
    }
    write_files(str(root_path / 'env') + '/path.yaml',data)

'''获取token'''
@pytest.fixture(scope='session', autouse=True)
def save_token():
        path = str(root_path / r'env' / r'test.yaml')
        save_path = str(root_path / r'env' / r'token.yaml')
        _env_data = parser_files(path)
        platform_data = _env_data['platform']
        user_data = _env_data['user']
        merchant_data = _env_data['merchant']
        # '''获取平台端token'''
        platform_send_data = {
            'method': 'post',
            'url': platform_data['host'] + '/api/admin/platform/login',
            'headers': {'content-type': 'application/json', },
            'json': {
                'account': platform_data['username'],
                'pwd': platform_data['password'],
                'captchaVO': {},
            }
        }

        # '''获取用户端token'''
        user_send_data = {
            'method': 'post',
            'url': user_data['host'] + '/api/front/login/mobile/password',
            'headers': {'content-type': 'application/json', },
            'json': {
                'phone': user_data['username'],
                'password': user_data['password'],
                'spreadPid': 0,
            }
        }

        # '''获取商户端token'''
        merchant_send_data = {
            'method': 'post',
            'url': merchant_data['host'] + '/api/admin/merchant/login',
            'headers': {'content-type': 'application/json', },
            'json': {
                'account': merchant_data['username'],
                'pwd': merchant_data['password'],
                'captchaVO': {},
                'captcha': {
                    'captchaVerification': '',
                    'secretKey': '',
                    'token': '',
                },
            }
        }
        res_mer = requests.request(**merchant_send_data)
        res_plat = requests.request(**platform_send_data)
        res_user = requests.request(**user_send_data)
        token_data = {
            'platform_token' : res_plat.json()['data']['token'],
            'user_token' : res_user.json()['data']['token'],
            'merchant_token' : res_mer.json()['data']['token']
        }
        write_files(save_path,token_data)



